from rest_framework import serializers
from .models import Area, Cinema


class AreaListSerializer(serializers.ModelSerializer):
    """城市区域列表序列化器（简化版）"""

    # area_type_display = serializers.ReadOnlyField(source='area_type_display')

    class Meta:
        model = Area
        fields = [
            'area_id', 'parent_id', 'area_name', 'area_type',
            'sort', 'hot', 'pinyin'
        ]


class CinemaListSerializer(serializers.ModelSerializer):
    """影院列表序列化器"""
    area_info = serializers.SerializerMethodField()

    class Meta:
        model = Cinema
        fields = [
            'id', 'store_id', 'title', 'img', 'address', 'avg_price',
            'area_id', 'area_info', 'work_time', 'tel', 'sort','seat'
        ]

    def get_area_info(self, obj):
        """获取影院所属区域信息"""
        area = obj.get_area_info()
        if area:
            return {
                'area_id': area.area_id,
                'area_name': area.area_name,
                'area_type': area.area_type,
                'pinyin': area.pinyin
            }
        return None


class CinemaSerializer(serializers.ModelSerializer):
    """影院详情序列化器"""
    area_info = serializers.SerializerMethodField()

    class Meta:
        model = Cinema
        fields = [
            'id', 'store_id', 'type', 'cat_id', 'area_id', 'img', 'imgs', 'title',
            'comment_count', 'comment_bad_count', 'comment_good_count', 'sell_count',
            'work_time', 'tel', 'address', 'lat', 'lng', 'start_price',
            'send_price', 'avg_price', 'sort', 'state', 'description',
            'created_at', 'updated_at', 'area_info', 'seat'
        ]

    def get_area_info(self, obj):
        """获取影院所属区域信息"""
        area = obj.get_area_info()
        if area:
            return {
                'area_id': area.area_id,
                'area_name': area.area_name,
                'area_type': area.area_type,
                'pinyin': area.pinyin
            }
        return None
